package com.sky.controller.user;

import com.sky.dto.AiChatDTO;
import com.sky.entity.AiChatRecord;
import com.sky.entity.Faq;
import com.sky.result.Result;
import com.sky.service.AiChatService;
import com.sky.service.FaqService;
import com.sky.vo.AiChatVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * AI聊天控制器
 */
@RestController
@RequestMapping("/user/ai")
@Api(tags = "AI聊天相关接口")
@Slf4j
public class AiChatController {

    @Autowired
    private AiChatService aiChatService;
    
    @Autowired
    private FaqService faqService;

    /**
     * 获取AI回答
     * @param requestData 包含问题的请求数据
     * @return 包含AI回答的Result
     */
    @PostMapping("/question")
    @ApiOperation("获取AI回答")
    public Result<AiChatVO> question(@RequestBody AiChatDTO requestData) {
        log.info("获取AI回答，问题: {}", requestData.getQuestion());
        AiChatVO vo = aiChatService.getAiAnswer(requestData);
        return Result.success(vo);
    }

    /**
     * 获取聊天历史记录
     * @param userId 用户ID
     * @return 聊天记录列表
     */
    @GetMapping("/history")
    @ApiOperation("获取聊天历史记录")
    public Result<List<AiChatRecord>> getHistory(@RequestParam(required = false) Long userId) {
        log.info("获取用户聊天历史记录，用户ID: {}", userId);
        List<AiChatRecord> list = aiChatService.getChatHistory(userId);
        return Result.success(list);
    }
    
    /**
     * 获取常见问题列表
     * @return FAQ列表
     */
    @GetMapping("/faq/list")
    @ApiOperation("获取常见问题列表")
    public Result<List<Faq>> getFaqList() {
        log.info("获取常见问题列表");
        // 只获取启用状态的FAQ
        List<Faq> list = faqService.listAll().stream()
                .filter(faq -> faq.getStatus() == 1)
                .collect(Collectors.toList());
        return Result.success(list);
    }
} 